package com.megurokai.paint;

import java.util.ArrayList;
import java.util.List;

import com.megurokai.paint04.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.SurfaceView;
import android.view.View;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class PaintActivity extends Activity {
	protected ScaleGestureDetector detector;
	protected PaintView paintView;
	protected SurfaceView surface;
	protected SelectableIcon iconBrush;
	protected SelectableIcon iconZoom;
	protected SelectableIcon iconScroll;
	protected SelectableIcon iconSubBrush;
	protected SelectableIcon iconSubPencil;
	protected Icon iconUndo;
	protected Icon iconRedo;
	protected List<SelectableIcon> mainIconList;
	protected List<SelectableIcon> brushIconList;
	protected SeekBar sbStrokeWidth;
	protected SeekBar sbAlpha;
	
	protected boolean onScale;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surface = (SurfaceView)findViewById(R.id.surface_view);
        paintView = new PaintView(this, surface);
        final PaintManager manager = PaintManager.getInstance();
        manager.setView(paintView);
        
		detector = new ScaleGestureDetector(this, onScaleGestureListener);
		onScale = false;
		
		// メインツールバーの初期化
		mainIconList = new ArrayList<SelectableIcon>();
		mainIconList.add(iconBrush = new SelectableIcon((ImageView)findViewById(R.id.brush), R.drawable.brush, R.drawable.brush_s));
		mainIconList.add(iconZoom = new SelectableIcon((ImageView)findViewById(R.id.zoom), R.drawable.zoom, R.drawable.zoom_s));
		mainIconList.add(iconScroll = new SelectableIcon((ImageView)findViewById(R.id.scroll), R.drawable.scroll, R.drawable.scroll_s));
		iconUndo = new Icon((ImageView)findViewById(R.id.undo), R.drawable.undo);
		iconRedo = new Icon((ImageView)findViewById(R.id.redo), R.drawable.redo);
		
		// ブラシツールバーの初期化
		brushIconList = new ArrayList<SelectableIcon>();
		brushIconList.add(iconSubBrush = new SelectableIcon((ImageView)findViewById(R.id.sub_brush), R.drawable.brush, R.drawable.brush_s));
		brushIconList.add(iconSubPencil = new SelectableIcon((ImageView)findViewById(R.id.sub_pencil), R.drawable.pencil, R.drawable.pencil_s));
		sbStrokeWidth = (SeekBar)findViewById(R.id.stroke_width);
		sbStrokeWidth.setOnSeekBarChangeListener(onSeekBarChangeStrokeWidth);
		sbStrokeWidth.setMax(50);
		sbStrokeWidth.setProgress(3);
		sbAlpha = (SeekBar)findViewById(R.id.alpha);
		sbAlpha.setOnSeekBarChangeListener(onSeekBarChangeAlpha);
		sbAlpha.setMax(255);
		sbAlpha.setProgress(255);
		
		// 初期ツールをセット
		resetIcon(mainIconList);
		resetIcon(brushIconList);
		onClickSubBrush(null);
		
		
    }

    // リスト内のアイコンを非選択表示にする
    private void resetIcon(List<SelectableIcon> list){
    	for(SelectableIcon icon : list){
    		icon.deselect();
    	}
    }
    
    // リスト内のアイコンを選択する
    private void selectIcon(List<SelectableIcon> list, SelectableIcon icon){
    	resetIcon(list);
    	icon.select();
    }
    
    // メインのブラシを選択
    public void onClickBrush(View view){
		paintView.setBrush();
    	selectIcon(mainIconList, iconBrush);
    }
    
    // メインのズームを選択
    public void onClickZoom(View view){
		paintView.setZoom();
		selectIcon(mainIconList, iconZoom);
    }
    
    // メインのスクロールを選択
    public void onClickScroll(View view){
		paintView.setScroll();
		selectIcon(mainIconList, iconScroll);
    }

    // サブのブラシを選択
    public void onClickSubBrush(View view){
		paintView.setBrush();
    	iconBrush.setImage(iconSubBrush);
		selectIcon(mainIconList, iconBrush);
    	selectIcon(brushIconList, iconSubBrush);
    }

    // サブの鉛筆を選択
    public void onClickSubPencil(View view){
		paintView.setPencil();
    	iconBrush.setImage(iconSubPencil);
    	selectIcon(mainIconList, iconBrush);
    	selectIcon(brushIconList, iconSubPencil);
    }
    
    // メインのUndoを選択
    public void onClickUndo(View view){
		paintView.undo();
    }
    
    // メインのRedoを選択
    public void onClickRedo(View view){
		paintView.redo();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	
    	//paintView.onOptionMenu();
    	return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        if(onScale == false){
    		switch(event.getAction()){
    		case MotionEvent.ACTION_UP:
    			paintView.onTouchUp(event);
    			break;
    		case MotionEvent.ACTION_DOWN:
    			paintView.onTouchDown(event);
    			break;
    		case MotionEvent.ACTION_MOVE:
    			paintView.onTouchMove(event);
    			break;
    		}
        }
        return false;
    }

    private final OnSeekBarChangeListener onSeekBarChangeStrokeWidth = new OnSeekBarChangeListener(){
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			paintView.setStrokeWidth(progress);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// 何もしない
		}
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// 何もしない
		}
    };
    
    private final OnSeekBarChangeListener onSeekBarChangeAlpha = new OnSeekBarChangeListener(){
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			paintView.setAlpha(progress);
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// 何もしない
		}
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// 何もしない
		}
    };
    
    private final SimpleOnScaleGestureListener onScaleGestureListener = new SimpleOnScaleGestureListener(){
		@Override
		public boolean onScale(ScaleGestureDetector det) {
			paintView.onScale(det);
			onScale = false;
			return true;
		}
	
		@Override
		public boolean onScaleBegin(ScaleGestureDetector det) {
			paintView.onScaleBegin(det);
			onScale = true;
			return true;
		}
	
		@Override
		public void onScaleEnd(ScaleGestureDetector det) {
			paintView.onScaleEnd(det);
			onScale = false;
		}
    };
}